package com.cndw.kungfu.business.gm;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.cndw.commons.util.BlankUtils;
import com.cndw.kungfu.comm.GmCommand;
import com.cndw.kungfu.comm.LocalCommand;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.Union;
import com.cndw.kungfu.domain.UnionMember;
import com.cndw.kungfu.ext.UtilsOfBusiness;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.union.UnionManager;
import com.cndw.kungfu.vo.UnionVO;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.annotation.UnAuth;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

@Controller
public class BusinessUnion {

	@Autowired
	private UnionManager unionManager;

	@UnAuth
	@GmCommand
	public ResponseMessage getUnion(Session session, String sign, int type, String param) {

		Union union = null;
		Long playerId = null;
		switch (type) {
		case 1:
			playerId = Long.parseLong(param);
			union = Context.getUnionServer().getUnionByPlayerId(playerId);
			break;
		case 2:
			playerId = Context.getPlayerServer().getPlayerIdByAccount(param);
			union = Context.getUnionServer().getUnionByPlayerId(playerId);
			break;
		case 3:
			Player player = Context.getPlayerServer().getByPlayerName(param);
			playerId = player.getPlayerId();
			union = Context.getUnionServer().getUnionByPlayerId(player.getPlayerId());
			break;
		}

		if (!BlankUtils.isBlank(union)) {
			UnionVO unionVO = union.toUnionVO();
			unionVO.setPlayerResource(union.getMember(playerId).getResource());
			return ResponseMessage.getSuccMessage(unionVO);
		}
		return ResponseMessage.getSuccMessage(null);
	}

	@UnAuth
	@LocalCommand
	public ResponseMessage updateUnion(Session session, String sign, Map<String, Object> sendMap) {
		if (sendMap.containsKey("unionId")) {
			int unionId = Integer.valueOf(sendMap.get("unionId").toString());
			Union union = Context.getUnionServer().getUnionById(unionId);
			synchronized (union) {
				UtilsOfBusiness.execUpdate(union, sendMap);
				unionManager.updateUnion(union);

				if (sendMap.containsKey("classId") && sendMap.containsKey("value") && sendMap.containsKey("playerResource")) {
					int type = Integer.parseInt(sendMap.get("classId").toString());
					String value = sendMap.get("value").toString();
					Long playerId = null;
					switch (type) {
					case 1:
						playerId = Long.parseLong(value);
						break;
					case 2:
						playerId = Context.getPlayerServer().getPlayerIdByAccount(value);
						break;
					case 3:
						Player player = Context.getPlayerServer().getByPlayerName(value);
						playerId = player.getPlayerId();
						break;
					}
					UnionMember member = union.getMember(playerId);
					member.setResource(Integer.parseInt(sendMap.get("playerResource").toString()));
					member.setHissource(Integer.parseInt(sendMap.get("playerResource").toString()));
					unionManager.updateMemberResource(member);
				}
			}
		}
		return ResponseMessage.getSuccMessage("ok");
	}
}
